package com.bj58.data.emailreports.controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bj58.data.emailreports.model.FCKTWeekReportCityVo;
import com.bj58.data.emailreports.model.FCKTWeekReportSpreadVo;
import com.bj58.data.emailreports.model.FCKTWeekReportTotalVo;
import com.bj58.data.emailreports.service.FCEmailReportService;
import com.bj58.data.emailreports.utils.DateUtil;
import com.bj58.data.emailreports.utils.EmailReportUtil;
import com.bj58.data.emailreports.utils.ResourcesUtil;
import com.bj58.data.emailreports.utils.SomePublicUtil;
import com.bj58.data.emailreports.utils.SortUtil;
import com.bj58.data.emailreports.utils.VelocityTools;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.groupbuy.util.PathUtil;

/**房产 邮件报告
 * @author sunny3791
 *
 */
public class FCEmailReportController {
	
	private FCEmailReportService fcEmailReportService= ServiceLocator.get(FCEmailReportService.class);
	private EmailReportUtil eru = new EmailReportUtil();
	private SomePublicUtil spu = new SomePublicUtil();
	
	/**开天项目周报
	 */
	public String KTWeekReport(String searchDate){
		try {
			//读取模板内容
			String filepath =PathUtil.getWFNamespaceFolder()+"/emailtemp/KTWeekReport.html";
			String templateContext = ResourcesUtil.getTemByPath(filepath);
			//渲染MAP
			Map<String,Object> conMap = new HashMap<String,Object>();
			//查询日期
			Date  sDate = DateUtil.strToDate(searchDate, "yyyy-MM-dd");
			String today = DateUtil.getStringDate(sDate,"yyyy-MM-dd");//查询日期（今）
			String lastWeek = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 7),"yyyy-MM-dd");//查询日期（上周）
			String lastMonth = DateUtil.getStringDate(DateUtil.getDateBefore(sDate, 30),"yyyy-MM-dd");//查询日期（上月）
			
			List<String> dateList = new ArrayList<String>();
			dateList.add(today);dateList.add(lastWeek);dateList.add(lastMonth);
			
			Map<String,String> dateMap = new HashMap<String, String>();
			dateMap.put("nowDate",today);
			dateMap.put("lastDate", lastWeek);
			dateMap.put("lastWeek",lastMonth);
			
			
			List<String> percentList = new ArrayList<String>(); //百分率字段
			List<String> showCols = new ArrayList<String>();  //展示字段
			List<String>  totalVoDicList = spu.getECDicByAnnotation(FCKTWeekReportTotalVo.class);
			
			
			
			/**  汇总数据 - 全国 **/
			List<FCKTWeekReportTotalVo> totalDataList = fcEmailReportService.ktTotalData(today, lastWeek, lastMonth, 0);
			
			List<String>  totaldicsList = new ArrayList<String>();
			for (String col : totalVoDicList) {
				if(col.contains("totalHouseInfo")){
					break;
				}
				totaldicsList.add(col);
			}
			List<String> totalDatas = eru.conformDataTypeOne(totalDataList, FCKTWeekReportTotalVo.class, null, dateMap, "statDate", totaldicsList);
			conMap.put("totalDatas", totalDatas);
			
			
			/**  汇总数据 - 北京 **/
			List<FCKTWeekReportTotalVo> totalDataBJList = fcEmailReportService.ktTotalData(today, lastWeek, lastMonth, 1);
			List<String> totalBJDatas = eru.conformDataTypeOne(totalDataBJList, FCKTWeekReportTotalVo.class, null, dateMap, "statDate", totalVoDicList);
			conMap.put("totalBJDatas", totalBJDatas);
			
			
			/**  新增房源量价格段分布 **/
			List<FCKTWeekReportSpreadVo>  houseInfoSpreadList = fcEmailReportService.ktHouseInfoSpreadData(today, lastWeek, lastMonth);
			showCols.clear();
			showCols.add("newHouseInfo");showCols.add("newHouseInfoPro");
			percentList.clear();
			percentList.add("newHouseInfoPro");
			
			List<String> houseInfoSpreadDatas = eru.conformDataTypeFour(houseInfoSpreadList, FCKTWeekReportSpreadVo.class, "type", showCols, "statDate", percentList, dateList);
			houseInfoSpreadDatas = SortUtil.sortListByFileName(houseInfoSpreadDatas, 1, 2);
			conMap.put("houseInfoSpreadDatas", houseInfoSpreadDatas);
			
			
			
			Map<String,List<String>> spreadMap = new HashMap<String, List<String>>(); //点击分布数据MAP
			/**  分类别点击分布 -  价格段 **/
			showCols.clear();
			showCols.add("detailPv");showCols.add("clickHouseInfo");showCols.add("detailPvPro");
			percentList.clear();
			percentList.add("detailPvPro");
			
			List<FCKTWeekReportSpreadVo>  priceSpreadList = fcEmailReportService.ktClickSpreadData(today, lastWeek, lastMonth,0);
			List<String> priceSpreadDatas = eru.conformDataTypeFour(priceSpreadList, FCKTWeekReportSpreadVo.class, "type", showCols, "statDate", percentList, dateList);
			priceSpreadDatas = SortUtil.sortListByFileName(priceSpreadDatas, 1, 2);
			spreadMap.put("价格段", priceSpreadDatas);
			
			
			/**  分类别点击分布 -  区域 **/
			List<FCKTWeekReportSpreadVo>  areaSpreadList = fcEmailReportService.ktClickSpreadData(today, lastWeek, lastMonth,1);
			List<String> areaSpreadDatas = eru.conformDataTypeFour(areaSpreadList, FCKTWeekReportSpreadVo.class, "type", showCols, "statDate", percentList, dateList);
			areaSpreadDatas = SortUtil.sortListByFileName(areaSpreadDatas, 1, 2);
			spreadMap.put("区域", areaSpreadDatas);
			
			
			/**  分类别点击分布 -  客户类型 **/
			List<FCKTWeekReportSpreadVo>  userTypeSpreadList = fcEmailReportService.ktClickSpreadData(today, lastWeek, lastMonth,2);
			List<String> userTypeSpreadDatas = eru.conformDataTypeFour(userTypeSpreadList, FCKTWeekReportSpreadVo.class, "type", showCols, "statDate", percentList, dateList);
			userTypeSpreadDatas = SortUtil.sortListByFileName(userTypeSpreadDatas, 1, 2);
			spreadMap.put("客户类型", userTypeSpreadDatas);
			
			
			conMap.put("spreadMap", spreadMap);
			
			/**  分城市流量分布 **/
			List<FCKTWeekReportCityVo> cityFluxList = fcEmailReportService.ktCityFluxData(today, lastWeek, lastMonth);
			showCols.clear();
			showCols.add("detailPv");showCols.add("uv");showCols.add("jumpOutRate");showCols.add("visitDeep");
			percentList.clear();
			percentList.add("jumpOutRate");
			
			List<String> cityFluxDatas = eru.conformDataTypeFour(cityFluxList, FCKTWeekReportCityVo.class, "city", showCols, "statDate", percentList, dateList);
			cityFluxDatas = SortUtil.sortListByFileName(cityFluxDatas, 1, 2);
			conMap.put("cityFluxDatas", cityFluxDatas);
			
			
			
			conMap.put("today", today.replaceAll("-", "."));
			conMap.put("lastWeek", lastWeek.replaceAll("-", "."));
			conMap.put("lastMonth", lastMonth.replaceAll("-", "."));
			return VelocityTools.render(templateContext, conMap);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
}
